<?php

App::uses('CrudController', 'Controller');

class AreasController extends CrudController
{
    public $_model = 'Area';

    public function admin($moeId) {
        $this->request->allowMethod('get');
        $this->loadModel('Moe');
        $appBosch = new stdClass();
        $appBosch->moeId = $moeId;
        $description = $this->getDescription($moeId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Áreas'));
        $this->set('description', $description);
    }

    private function getDescription($moeId) {
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        return $moe['Moe']['name'];
    }

    public function getAreasByMoe($all = null)
    {
        $this->request->allowMethod('get');
        $moeId = isset($this->request->query['filter']['filters'][0]['value']) ? $this->request->query['filter']['filters'][0]['value'] : '';
        $m = $this->_model;
        $records = $this->$m->getEnabledByMoe($moeId);
        if ($all != null) {
            array_unshift($records, array("text" => 'Todas', "value" => '0'));
        }
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getAreasByMoeByParam($moeId)
    {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $records = $this->$m->getEnabledByMoe($moeId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getAreasForCombo()
    {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $records = $this->$m->getEnabled();
        array_unshift($records, array("text" => 'Todas', "value" => '0'));
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param) {
        $m = $this->_model;
        $records = $this->$m->getEnabledByMoe($param);
        return $records;
    }

    protected function c($model) {
        return array(
            'moe_id' => $model->moe_id,
            'name' => $model->text,
            'text' => $model->text,
            'color' => $model->color
        );
    }

    protected function id($model) {
        return $model->value;
    }

    protected function u($model) {
        return array(
            'moe_id' => $model->moe_id,
            'name' => $model->text,
            'text' => $model->text,
            'color' => $model->color
        );
    }
}